package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.adapter.ActionDetailAdapter;
import com.hejian.android.a72ggametools.bean.ActionComment;
import com.hejian.android.a72ggametools.bean.ActionInfo;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/22.
 */
public class ActionActivity extends AppCompatActivity {
    private ListView actionListView;
    private View actionListHead;
    private Intent intent;
    private String id;
    private TextView actionTitle;
    private TextView actionWriter;
    private TextView actionPubdate;
    private TextView actionContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);
        initView();
        initData();
    }

    private void initData() {
        intent = getIntent();
        id = intent.getStringExtra("id");
        loadData();
    }

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadHeadData();
                loadListData();
            }
        }).start();
    }

    private void loadListData() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);
        String buffer = HttpUtils.doPostRequest(Constant.ACTION_DETAIL_COMMENT_URL,map);
        List<ActionComment> list = JsonParse.parseActionCommentJson(buffer);
        Message msg = Message.obtain();
        msg.what = 2;
        msg.obj = list;
        handler.sendMessage(msg);

    }

    private void loadHeadData() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);
        String buffer = HttpUtils.doPostRequest(Constant.ACTION_DETAIL_HEAD_URL,map);
        ActionInfo info = JsonParse.parseActionInfoJson(buffer);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = info;
        handler.sendMessage(msg);

    }

    private void initView() {
        actionListView = (ListView)findViewById(R.id.action_listview);
        actionListHead = LayoutInflater.from(this).inflate(R.layout.action_listview_head,null);
        actionTitle = (TextView) actionListHead.findViewById(R.id.action_detail_title);
        actionWriter = (TextView)actionListHead.findViewById(R.id.action_detail_writer);
        actionPubdate = (TextView)actionListHead.findViewById(R.id.action_detail_pubdate);
        actionContent = (TextView)actionListHead.findViewById(R.id.action_detail_content);
        actionListView.addHeaderView(actionListHead);
    }

    private ActionInfo actionInfo;
    private List<ActionComment> actionCommentList;
    private ActionDetailAdapter adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    actionInfo = (ActionInfo) msg.obj;
                    actionTitle.setText(actionInfo.getaName());
                    actionWriter.setText(actionInfo.getWriter());
                    actionPubdate.setText(actionInfo.getPubdate());
                    actionContent.setText(actionInfo.getContent());
                    break;
                case 2:
                    actionCommentList = (List<ActionComment>) msg.obj;
                    adapter = new ActionDetailAdapter(actionCommentList,getApplicationContext());
                    actionListView.setAdapter(adapter);
            }
        }
    };
}
